<?php

// no direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.controllerform');

/**
 * @package		Joomla.Site
 * @subpackage	com_weblinks
 * @since		1.5
 */
class ForumControllerPost extends JControllerForm
{
	protected $view_item = 'post';
	

	function save()
	{

		JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

		// Initialise variables.
		$app	= JFactory::getApplication();
		$params		= $app->getParams();
		$model	= $this->getModel('Post', 'ForumModel');
		$user	= JFactory::getUser();
		$userId	= (int) $user->get('id');

		// Get the user data.
		$data = JRequest::getVar('jform', array(), 'post', 'array');
		$catid = $data['catid'];
		
		// Validate the posted data.
		$form = $model->getForm();
		if (!$form) {
			JError::raiseError(500, $model->getError());
			return false;
		}

		$data['title'] = substr(strip_tags($data['message']), 0, 50). "...";
		// Validate the posted data.
		$data = $model->validate($form, $data);

		// Check for errors.
		if ($data === false) {
			// Get the validation messages.
			$errors	= $model->getErrors();

			// Push up to three validation messages out to the user.
			for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) {
				if ($errors[$i] instanceof Exception) {
					$app->enqueueMessage($errors[$i]->getMessage(), 'warning');
				} else {
					$app->enqueueMessage($errors[$i], 'warning');
				}
			}

			// Save the data in the session.
			$app->setUserState('com_forum.post.data', $data);

			// Redirect back to the edit screen.
			$this->setRedirect(JRoute::_('index.php?option=com_forum&view=topic&catid='.$catid.'&id=' .$data['tid'], false));
			return false;
		}

		
		
		//captcha control
		//require_once JPATH_COMPONENT.'/helpers/route.php'; 
		
		if($params->get('enable_recaptcha',1) && $privateKey = $params->get('recaptcha_private_key', false) && $publicKey = $params->get('recaptcha_public_key', false))
		{
			$privateKey = $params->get('recaptcha_private_key');
			require_once JPATH_COMPONENT.'/helpers/recaptchalib.php'; 
			
			$resp = recaptcha_check_answer ($privateKey,
										$_SERVER["REMOTE_ADDR"],
										$_POST["recaptcha_challenge_field"],
										$_POST["recaptcha_response_field"]);

			if (!$resp->is_valid) {
			// What happens when the CAPTCHA was entered incorrectly
				$app->enqueueMessage(JText::_("COM_FORUM_POST_CAPTCHA_ERROR"), 'warning');
			
				$app->setUserState('com_forum.post.data', $data);

				// Redirect back to the edit screen.
				
				$this->setRedirect(JRoute::_(('index.php?option=com_forum&view=post&layout=add&catid='.$catid.'&tid=' .$data['tid']), false));
				return false;
			} 
		}

		if($userId != 0)
		{
			$data['created_by_alias'] = $user->get('name');
			$data['created_by'] = $userId;
		}
		// Attempt to save the data.
		$return	= $model->save($data);
		
		// Check for errors.
		if ($return === false) {
			// Save the data in the session.
			$app->setUserState('com_forum.post.data', $data);

			// Redirect back to the edit screen.
			$this->setMessage(JText::sprintf('COM_FORUM_POST_SAVE_FAILED', $model->getError()), 'warning');
			$this->setRedirect(JRoute::_('index.php?option=com_forum&view=category&id='.$catid, false));
			return false;
		}

		// Redirect the user and adjust session state based on the chosen task.
		switch ($this->getTask()) {
			case 'apply':
				// Check out the profile.
				$app->setUserState('com_forum.post.id', $return);

				// Redirect back to the edit screen.
				$this->setMessage(JText::_('COM_FORUM_POST_SAVE_SUCCESS'));
				$this->setRedirect(JRoute::_('index.php?option=com_forum&view=category&id='.$catid, false));
				break;

			default:

				// Clear the profile id from the session.
				$app->setUserState('com_forum.post.catid', null);

				// Redirect to the list screen.
				$this->setMessage(JText::_('COM_FORUM_POST_SAVE_SUCCESS'));
				
				$this->setRedirect(JRoute::_('index.php?option=com_forum&view=category&id='.$catid, false));
				break;
		}

		// Flush the data from the session.
		$app->setUserState('com_forum.post.data', null);		
		
	}

}